home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / scripts / signal / fftfilt.m < prev    next >
Text File  |  1996-07-15  |  3KB  |  98 lines

  1. ## Copyright (C) 1996 John W. Eaton
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## usage:  fftfilt (b, x [, N])
  21. ##
  22. ## y = fftfilt (b, x) filters x with the FIR filter b using the FFT.
  23. ## y = fftfilt (b, x, N) uses the overlap-add method to filter x with
  24. ## b using an N-point FFT.
  25. ##
  26. ## Reference:  Oppenheim & Schafer (1989).  Discrete-time Signal
  27. ## Processing (Chapter 8).  Prentice-Hall.
  28.  
  29. ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
  30. ## Created: 3 September 1994
  31. ## Adapted-By: jwe
  32.  
  33. function y = fftfilt (b, x, N)
  34.  
  35.   ## If N is not specified explicitly, we do not use the overlap-add
  36.   ## method at all because loops are really slow.  Otherwise, we only
  37.   ## ensure that the number of points in the FFT is the smallest power
  38.   ## of two larger than N and length(b).  This could result in length
  39.   ## one blocks, but if the user knows better ...
  40.  
  41.   if (nargin < 2 || nargin > 3)
  42.     usage (" fftfilt (b, x [, N])");
  43.   endif
  44.  
  45.   [r_x, c_x] = size (x);
  46.   [r_b, c_b] = size (b);
  47.   if (! (min ([r_x, c_x]) == 1 || min ([r_b, c_b]) == 1))
  48.     error ("fftfilt: both x and b should be vectors");
  49.   endif
  50.   l_x  = r_x * c_x;
  51.   l_b  = r_b * c_b;
  52.  
  53.   if ((l_x == 1) && (l_b == 1))
  54.     y = b * x;
  55.     return;
  56.   endif
  57.  
  58.   x = reshape (x, 1, l_x);
  59.   b = reshape (b, 1, l_b);
  60.  
  61.   if (nargin == 2)
  62.     ## Use FFT with the smallest power of 2 which is >= length (x) +
  63.     ## length (b) - 1 as number of points ...
  64.     N    = 2^(ceil (log (l_x + l_b - 1) / log(2)));
  65.     y    = ifft (fft (x, N) .* fft(b, N));
  66.   else
  67.     ## Use overlap-add method ...
  68.     if !(is_scalar (N))
  69.       error ("fftfilt: N has to be a scalar");
  70.     endif
  71.     N = 2^(ceil (log (max ([N, l_b])) / log(2)));
  72.     L = N - l_b + 1;
  73.     B = fft (b, N);
  74.     R = ceil (l_x / L);
  75.     y = zeros (1, l_x);
  76.     for r = 1:R;
  77.       lo  = (r - 1) * L + 1;
  78.       hi  = min (r * L, l_x);
  79.       tmp = ifft (fft (x(lo:hi), N) .* B);
  80.       hi  = min (lo+N-1, l_x);
  81.       y(lo:hi) = y(lo:hi) + tmp(1:(hi-lo+1));
  82.     endfor
  83.   endif
  84.  
  85.   y = reshape (y(1:l_x), r_x, c_x);
  86.  
  87.   ## Final cleanups:  if both x and b are real respectively integer, y
  88.   ## should also be
  89.  
  90.   if (! (any (imag (x)) || any (imag (b))))
  91.     y = real (y);
  92.   endif
  93.   if (! (any (x - round (x)) || any (b - round (b))))
  94.     y = round (y);
  95.   endif
  96.  
  97. endfunction
  98.